//
//  YXJMusicStoryCell.m
//  Piece
//
//  Created by dllo on 16/6/8.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "YXJMusicStoryCell.h"
#import "NSString+YXJHeight.h"

@implementation YXJMusicStoryCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.musicNameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_musicNameLabel];

        self.authorNameLabel = [[UILabel alloc] init];
        self.authorNameLabel.font = [UIFont systemFontOfSize:14];
        [self.authorNameLabel setTextColor:[UIColor colorWithRed:0.58 green:0.76 blue:0.91 alpha:1.00]];
        [self.contentView addSubview:_authorNameLabel];
        
        self.storyLabel = [[UILabel alloc] init];
        self.storyLabel.numberOfLines = 0;
        self.storyLabel.font = [UIFont systemFontOfSize:15];
        self.storyLabel.textColor = [UIColor colorWithWhite:0.098 alpha:1.000];
        [self.contentView addSubview:_storyLabel];

        self.edityLabel = [[UILabel alloc] init];
        self.edityLabel.textColor = [UIColor colorWithWhite:0.400 alpha:1.000];
        self.edityLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:_edityLabel];
        
        self.likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.likeButton setImage:[UIImage imageNamed:@"like_normal"] forState:UIControlStateNormal];
        self.isLike = NO;
        [self.likeButton addTarget:self action:@selector(didClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_likeButton];
        
        self.numLabel = [[UILabel alloc] init];
        self.numLabel.textColor = [UIColor colorWithWhite:0.400 alpha:1.000];
        self.numLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_numLabel];
        
        self.moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.moreButton addTarget:self action:@selector(didMoreButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.moreButton setImage:[UIImage imageNamed:@"share_image"] forState:UIControlStateNormal];
        [self.contentView addSubview:_moreButton];
        
    }
    return self;
}

- (void) didMoreButton:(UIButton *)sender
{
    NSLog(@"点击了more按钮");
}

- (void)didClicked:(UIButton *)sender
{
    if (self.isLike == NO) {
    [sender setImage:[UIImage imageNamed:@"like_selected"] forState:UIControlStateNormal];
    self.numLabel.text = [NSString stringWithFormat:@"%ld", self.model.praisenum + 1];
    self.isLike = YES;
    }
    else{
    [sender setImage:[UIImage imageNamed:@"like_normal"] forState:UIControlStateNormal];
    self.numLabel.text = [NSString stringWithFormat:@"%ld", self.model.praisenum];
    self.isLike = NO;
    }

}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
    if (self.page == 1) {
        self.musicNameLabel.frame = CGRectMake(10, 10, 250, 20);
        self.authorNameLabel.frame = CGRectMake(self.musicNameLabel.frame.origin.x, self.musicNameLabel.frame.origin.y + self.musicNameLabel.frame.size.height + 10, self.musicNameLabel.frame.size.width, 20);
        
        NSString *storyStr = self.model.story;
        CGRect newRect = [storyStr boundingRectWithSize:CGSizeMake(ScreenWidth - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        self.storyLabel.frame = CGRectMake(self.musicNameLabel.frame.origin.x, self.authorNameLabel.frame.origin.y + self.authorNameLabel.frame.size.height + 20, newRect.size.width, newRect.size.height + 10);
    }
    if (self.page == 2)
    {
        self.musicNameLabel.frame = CGRectZero;
        self.authorNameLabel.frame = CGRectZero;
        NSString *lyricStr = self.model.lyric;
        NSArray *lyricArray = [lyricStr componentsSeparatedByString:@"／"];
        NSString *lyricString = [lyricArray componentsJoinedByString:@"\n"];
        
        CGRect newRect = [lyricString boundingRectWithSize:CGSizeMake(ScreenWidth - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        self.storyLabel.frame = CGRectMake(10, 10,  newRect.size.width, newRect.size.height + 10);
        
    }
    
    if (self.page == 3)
    {
        self.musicNameLabel.frame = CGRectZero;
        self.authorNameLabel.frame = CGRectZero;
        NSString *aboutStr = self.model.info;
        CGRect newRect = [aboutStr boundingRectWithSize:CGSizeMake(ScreenWidth - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        self.storyLabel.frame = CGRectMake(10, 10, newRect.size.width, newRect.size.height + 10);
    }
    
    self.edityLabel.frame = CGRectMake(self.storyLabel.frame.origin.x, self.storyLabel.frame.origin.y + self.storyLabel.frame.size.height + 10, 280, 20);
    
    self.likeButton.frame = CGRectMake(_edityLabel.frame.origin.x + _edityLabel.frame.size.width, _edityLabel.frame.origin.y - 5, 32, 32);
    self.numLabel.frame = CGRectMake(_likeButton.frame.origin.x + _likeButton.frame.size.width + 5, _likeButton.frame.origin.y + 5, 40, 20);
    self.moreButton.frame = CGRectMake(_numLabel.frame.origin.x + _numLabel.frame.size.width, _numLabel.frame.origin.y - 5, 40, 30);
}

- (void)setModel:(YXJMusicModel *)model
{
    if (_model != model) {
        _model = model;
    }
    self.musicNameLabel.text = model.title;
    self.authorNameLabel.text = model.authorModel.user_name;
    
    //去除文字中的<br>
    NSString *html = model.story;
    NSScanner *scanner = [NSScanner scannerWithString:html];
    NSString *text = nil;
    while([scanner isAtEnd]==NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"<" intoString:nil];
        
        //找到标签的结束位置
        [scanner scanUpToString:@">" intoString:&text];
        
        //替换字符
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    }
    
    //歌词的拼接
    NSString *lyric = model.lyric;
    NSArray *lyricArray = [lyric componentsSeparatedByString:@"／"];
    NSString *lyricString = [lyricArray componentsJoinedByString:@"\n"];
    
    
    if (self.page == 1) {
        self.storyLabel.text = html;
    }
    if (self.page == 2) {
        
        self.storyLabel.text = lyricString;
    }
    if (self.page == 3) {
        self.storyLabel.text = model.info;
    }
    self.edityLabel.text = model.charge_edt;
    
    self.numLabel.text = [NSString stringWithFormat:@"%ld", model.praisenum];
}


@end
